# 这个函数帮助我们数据库查询渲染HTML页面,然后把渲染的HTML写入到指定文件
import time

from apps.contents.models import ContentCategory
from utils.goods import get_categories


def generic_meiduo_index():
    print('----------%s----------'%time.ctime())
    # 获取商品分类数据
    categories = get_categories()
    # 广告数据
    contents = {}
    content_categories = ContentCategory.objects.all()
    for cat in content_categories:
        contents[cat.key] = cat.content_set.filter(status=True).order_by('sequence')
    context = {
        'categories': categories,
        'contents': contents,
    }
    #加载渲染的模板
    from django.template import loader
    index_template = loader.get_template('index.html')
    #把数据给模板
    from django.conf import settings
    import os
    index_html_data = index_template.render(context)
    #写入到指定文件
    file_path = os.path.join(os.path.dirname(settings.BASE_DIR), 'front_end_pc/index.html')
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(index_html_data)
